package com.lemon.api.auto.cases;

import com.lemon.api.auto.pojo.Case;
import com.lemon.api.auto.util.ExcelUtil;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Test(enabled = false)
public class LoginCase {
    public void doGet(String url,List<NameValuePair>parameters,String charset){
        //转换格式用&连接起来
        String parameterString = URLEncodedUtils.format(parameters, charset);
        url +=("?"+parameterString);
        //接口url地址
/*        String url="http://test.lemonban.com/futureloan/mvc/api/member/login";
        String mobilephone="13517315120";
        String pwd="123456";
        url+=("?mobilephone="+mobilephone+"+&pwd="+pwd);*/
        //创建请求对象
        HttpGet httpGet = new HttpGet(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //添加请求头指定数据格式
        httpGet.addHeader("Content-Type","application/x-www-form-urlencoded;charest=UTF-8");
        //发包
        try {
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpGet);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Test(enabled = false)
    public static void doPost(String url, List<NameValuePair>parameters,String charset){
        //创建请求对象
        HttpPost httpPost = new HttpPost(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //发包
        try {
//            List<BasicNameValuePair> paramters = new ArrayList<BasicNameValuePair>();
            //设置请求头
            httpPost.addHeader(new BasicHeader("Content-Type","application/x-www-form-urlencoded;charest=UTF-8"));
            //准备参数，将参数设置到请求体中
            //UrlEncodedFormEntity:以表单的方式传递数据（mobilephone=13517315120&pwd=123456）
            String parameterString = URLEncodedUtils.format(parameters, charset);
            httpPost.setEntity(new StringEntity(parameterString,charset));
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpPost);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文 转换成字符串
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头 把多个请求头放到数组里面
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
@Test(enabled = true)
    public void test() {
        String url="http://192.168.8.206:7788/ews2/oauth/login.do";
        List<NameValuePair> paramters=new ArrayList<NameValuePair>();
        paramters.add(new BasicNameValuePair("mobilephone","13517315120"));
        paramters.add(new BasicNameValuePair("pwd","123456"));
        String charset ="UTF-8";
//        doPost(url,paramters,charset);
        doGet(url,paramters,charset);


    }

    @DataProvider(name = "dl")
    public Object[] datas(){
        int [] rows={2,3,4,5,6,7};
        int [] cells ={3,4,6};
        String filepath="src/test/resources/cases_v1.xls";
        List<Case> datas= ExcelUtil.read(filepath,"用例");
        return null;
    }

}
